<?php

namespace backend\controllers;

use Yii;

use yii\web\Controller;
use backend\models\Area;
class AreaController extends Controller{


	public $layout = 'common';
	public function actionAdd()
	{
		$areas = Area::find()->where(['parent_id'=>0])->asArray()->all();
		return $this->render('add',['areas'=>$areas]);
	}
	public function actionAdds()
	{
		$data = Yii::$app->request->get();
		$areas = new Area;
		$areas->area_name = $data['area'];
		$areas->parent_id = $data['pid'];
		$areas->area_level = $data['level'];
		$areas->insert();
		echo Yii::$app->db->getLastInsertID();
	}
	public function actionSelect()
	{
		$pid = Yii::$app->request->get('pid');
		$area = Area::find()->where(['parent_id'=>$pid])->asArray()->all();
		echo json_encode($area);
	}
	public function actionGetchild($data,$pid=0)
	{
		$arr = array();
		foreach($data as $key => $val){
			if($val['parent_id'] == $pid){
				$child = $this->getchild($data,$val['parent_id']);
				if($child){
					$val['child'] = $child;					
				}
				$arr[] = $val;
			}
		}
		return $arr;
	}
}